﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Workflow
//  File name:	WorkflowRuleExecutor.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using Smart.Common.Rules;

namespace Smart.Workflow.Rules
{
	public class WorkflowRuleExecutor : XmlRuleExecutor
	{
		public WorkflowRuleExecutor(RulesCtx ctx)
			: base(ctx)
		{
		}

		public override bool ApplyAction(XmlRuleActionMethod action)
		{
			var ari = Ctx.Instance as WorkflowActionRulesInstance;

			if (ari != null)
			{
				bool handled;

				var res = ari.Service.PerformAction(action.Name, action.Parameters, out handled);

				if (handled)
					return res;

				if (ari.Instance != null)
					res = ari.Instance.PerformAction(action.Name, action.Parameters, out handled);

				if (!handled)
					throw new NotSupportedException(action.Name);

				return res;
			}

			throw new NotSupportedException();
		}
	}
}